<template>
    <div class="login_div">
        <div class="login_container">
            <div class="login_avatar">
                <img src="../assets/logo.png"/>
            </div>
            <el-form class="login_form" ref="loginForm" :model="loginFormData" :rules="rules">
                <el-form-item label="" prop="username">
                    <el-input prefix-icon="el-icon-user" v-model="loginFormData.username"></el-input>
                </el-form-item>
                <el-form-item label="" prop="password">
                    <el-input prefix-icon="el-icon-aim" v-model="loginFormData.password"></el-input>
                </el-form-item>
                <el-form-item class="login_btns">
                    <el-button type="primary" plain @click='login'>登录</el-button>
                    <el-button type="info" plain @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      loginFormData: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          {
            min: 3, max: 10, message: '长度为3～10位', trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6, max: 16, message: '长度位6～16位', trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.loginForm.resetFields()
      console.log(this.loginFormData)
    },
    login () {
      this.$refs.loginForm.validate(async (valid) => {
        if (!valid) return false
        const { data: res } = await this.axios.post('/private/v1/login', this.loginFormData)
        if (!res || !res.meta) return this.$message.error('登陆失败')
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        const token = res.data.token
        window.sessionStorage.setItem('token', token)
        this.$message.success('登录成功')
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.login_div{
    background-color: rgb(179, 216, 255);
    height: 100%;
    .login_container{
        position: absolute;
        background-color: rgb(255, 255, 255);
        width: 380px;
        height: 280px;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        .login_avatar{
            width: 120px;
            height: 120px;
            background-color: rgb(255, 255, 255);
            border-radius: 50%;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 999;
            padding: 10px;
            box-shadow: 0 0 10px #FFF;
            img{
                height: 100%;
                width: 100%;
                border-radius: 50%;
                background-color: rgb(253, 252, 250)
            }
        }
        .login_form{
            position:absolute;
            bottom: 0;
            padding: 0 20px;
            width: 100%;
            box-sizing: border-box;
            .login_btns{
                display: flex;
                justify-content: flex-end;
            }
        }
    }
}
</style>
